package com.zlf.sb.demo.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.zlf.sb.demo.design.chain.ChainBizMarkEnum;
import com.zlf.sb.demo.design.chain.MerchantAdminChainContext;
import com.zlf.sb.demo.dto.ProcessedUserData;
import com.zlf.sb.demo.dto.UserEntityDto;
import com.zlf.sb.demo.entity.UserEntity;
import com.zlf.sb.demo.mapper.UserMapper;
import com.zlf.sb.demo.service.IUserService;
import com.zlf.sb.demo.service.ValueValidUtils;
import com.zlf.sb.demo.service.impl.UserDataProcessor;
import com.zlf.sb.demo.vo.ProductUpShelfReqDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;


/**
 * 示例前端请求控制器
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class DemoController {
    @Autowired
    private IUserService userService;
    @Autowired
    private UserDataProcessor userDataProcessor;
    @Autowired
    private MerchantAdminChainContext merchantAdminChainContext;
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/getUserListByPage")
    public void getUserListByPage() throws IOException {
        userService.queryInsertData();
    }

    @GetMapping("/cursorQueryInsertData")
    public void cursorQueryInsertData() throws IOException {
        userService.cursorQueryInsertData();
    }

    @GetMapping("/queryUserByPage")
    public Page queryUserByPage(UserEntityDto userEntityDto) throws IOException {
        Page<UserEntity> userEntityPage = userService.pageList(userEntityDto);
        return userEntityPage;
    }

    @RequestMapping("/processUserData")
    public ProcessedUserData processUserData(String userId) throws Exception {
        CompletableFuture<ProcessedUserData> processedUserDataCompletableFuture = userDataProcessor.processUserData(userId);
        ProcessedUserData processedUserData = processedUserDataCompletableFuture.get();
        return processedUserData;
    }

    @RequestMapping("/zrchain")
    public void zrchain(String userId) throws Exception {
        ProductUpShelfReqDTO productUpShelfReqDTO = new ProductUpShelfReqDTO();
        productUpShelfReqDTO.setProductName("111");
        productUpShelfReqDTO.setStock(10);
        productUpShelfReqDTO.setPrice(10);

        merchantAdminChainContext.handler(ChainBizMarkEnum.MERCHANT_ADMIN_PRODUCT_UPSHELF_KEY.name(), productUpShelfReqDTO);
    }

    @RequestMapping("/exception")
    public void exception(String userId) throws Exception {
        // ValueValidUtils.ensureColumnValueValid(userId, UserEntity::getId, userMapper::selectOne, "用户ID无效");
        ValueValidUtils.validateColumnValueMatchesExpected(UserEntity::getId, "普通用户", UserEntity::getId, userId, userMapper::selectOne, "用户角色不是普通用户，无法升级为管理员！");

        // merchantAdminChainContext.handler(ChainBizMarkEnum.MERCHANT_ADMIN_PRODUCT_UPSHELF_KEY.name(),productUpShelfReqDTO);

    }


    @GetMapping("/query")
    public ResponseEntity<List<UserEntity>> query() {
        ArrayList<Long> longs = Lists.newArrayList(1L, 2L, 3L, 4L, 5L);

        return ResponseEntity.ok(this.userService.findByAgeAndNameContainingAndIdIn(longs)) ;
    }
}
